package com.wawayaya.bookreader;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.wbtech.ums.UmsAgent;

import wawayaya.kids_iread.R;

public class TouchMoveButton extends Button {

    private View mParentLayout;
    private SettingBtnClicked  mOnClicked;

    private int          mTopHeight;
    private int          mBottomHeight;
    private int           mScreenWidth;
    private int          mScreenHeight;
    private Context mContext;
    private int          mMargin;
    private Handler mHandler;
    
	public TouchMoveButton(Context context) {
		super(context);
		mContext = context;
		mMargin = (int)mContext.getResources().getDimension(R.dimen.booksetting_btn_margin);
	}

	public TouchMoveButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		mMargin = (int)mContext.getResources().getDimension(R.dimen.booksetting_btn_margin);
	}

	public TouchMoveButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mMargin = (int)mContext.getResources().getDimension(R.dimen.booksetting_btn_margin);
	}
	
	public void setOnClicked(SettingBtnClicked onclick) {
		mOnClicked = onclick;
	}
	
	
	int  down_x = 0;
	int  down_y = 0;
	boolean  mFollowFinger = false;
	boolean  mMaybeClick = false;
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int x = (int)event.getRawX();
		int y = (int)event.getRawY();
		switch(event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			down_x = x; 
			down_y = y;
			mFollowFinger = true;
			mMaybeClick = true;
			//MyLog.d("reader_mark ACTION_DOWN " + x + " " + y );
			break;
		case MotionEvent.ACTION_MOVE:
			//MyLog.d("reader_mark ACTION_MOVE "  + x + " " + y);
			if (mMaybeClick && distanceBetweenPoints(x, y, down_x, down_y) < 10) {
				
			} else {
				mMaybeClick = false;
				if (mFollowFinger) {
					if (!changePosition(x, y)) {
						mFollowFinger = false;
						modifyPosition();
					}
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			//MyLog.d("reader_mark ACTION_UP "  + x + " " + y);
			if (mMaybeClick && distanceBetweenPoints(x, y, down_x, down_y) < 10) {
				if (mOnClicked != null) mOnClicked.onSettingBtnClicked();
			} else if (mFollowFinger)  {
				//changePosition(x, y);
				modifyPosition();
			}
			break;
		case MotionEvent.ACTION_CANCEL:
			//MyLog.d("reader_mark ACTION_CANCEL "  + x + " " + y);
			break;
		case MotionEvent.ACTION_OUTSIDE:
			//MyLog.d("reader_mark ACTION_OUTSIDE "  + x + " " + y);
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private double distanceBetweenPoints(float curX, float curY, float lastX, float lastY) {
		float disX = Math.abs(curX - lastX);
		float disY = Math.abs(curY - lastY);

		return Math.sqrt(disX * disX + disY * disY);
	}	
	
	public void setParentLayout(View l) {
		mParentLayout = l;
	}
	
	private boolean changePosition(int x, int y) {
		
		int parent_w = mParentLayout.getWidth();
		int parent_h = mParentLayout.getHeight();
		//MyLog.d("reader_mark changePosition " + "x = " + x + " y = " + y + " parent_w = " + parent_w + " parent_h = " + parent_h);
		int h = getHeight();
		int offsetY =  (parent_h - h ) / 2;
		y -= offsetY;
		int left_margin = x - (parent_w / 2);
		int top_margin = y - (parent_h / 2);
		

	    if (left_margin < 0 || top_margin < 0
	    		|| (left_margin + parent_w) > mScreenWidth 
	    		|| (top_margin + parent_h) > mScreenHeight) {
	    	return false;
	    }
	    
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mParentLayout.getLayoutParams();
		lp.leftMargin = left_margin;
		lp.topMargin = top_margin;
		mParentLayout.setLayoutParams(lp);
		
		return true;
	}
	
	public void modifyPosition() {
		final int dst_x;
		final int dst_y;
		UmsAgent.onEvent(mContext, "Toolbar_drag");
		int parent_w = mParentLayout.getWidth();
		int parent_h = mParentLayout.getHeight();
		final RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mParentLayout.getLayoutParams();
		if ((lp.leftMargin + parent_w / 2) <  mScreenWidth / 2) {
			dst_x = mMargin;
		} else {
			dst_x = mScreenWidth - mMargin - parent_w;
		}
		
		if ((lp.topMargin > mTopHeight + mMargin) && (lp.topMargin + parent_h < mScreenHeight - mBottomHeight - mMargin)) {
			dst_y = lp.topMargin;
		} else if (lp.topMargin < mTopHeight + mMargin) {
			dst_y = mTopHeight + mMargin;
		} else {
			dst_y = mScreenHeight - mBottomHeight - mMargin - parent_h ;
		}
		
		TranslateAnimation anim = new TranslateAnimation(0, dst_x - lp.leftMargin , 0, dst_y - lp.topMargin);
		anim.setDuration(300);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				Message msg = mHandler.obtainMessage(0, dst_x, dst_y);
				mHandler.sendMessage(msg);
				//lp.leftMargin = dst_x;
				//lp.topMargin = dst_y;
				//mParentLayout.setLayoutParams(lp);
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				//mParentLayout.setEnabled(false);
			}} );
		mParentLayout.startAnimation(anim);		
	}
	
	public void setTopBottomView(int topHeight, int bottomHeight) {

		mTopHeight = topHeight;
		mBottomHeight = bottomHeight;
	}
	
	public void setSceenSize(int SceenWidth, int SceenHeight) {
		mScreenWidth = SceenWidth;
		mScreenHeight = SceenHeight;
	}
	

	
	public void setHandler(Handler handle) {
		mHandler = handle;
	}
	
	
	


}
